package com.mach.platform.service;

import com.google.common.collect.Lists;
import com.mach.platform.domain.ModifyOrderLogs;
import com.mach.platform.domain.Order;
import com.mach.platform.domain.base.Account;
import com.mach.platform.repository.ModifyOrderLogsRepo;
import com.mach.platform.utils.BeanChangeUtil;
import com.mach.platform.utils.OrderDict;
import com.mach.platform.utils.springdata.DynamicSpecifications;
import com.mach.platform.utils.springdata.SearchFilter;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class ModifyOrderLogsServiceImpl implements BaseService<ModifyOrderLogs>  {
    @Autowired
    private ModifyOrderLogsRepo modifyOrderLogsRepo;
    @Override
    public ModifyOrderLogs save(ModifyOrderLogs modifyOrderLogs) {
        return null;
    }

    @Override
    public List<ModifyOrderLogs> insertObjInBatch(List<ModifyOrderLogs> list) {
        return null;
    }

    @Override
    public ModifyOrderLogs update(ModifyOrderLogs modifyOrderLogs) {
        return null;
    }

    @Override
    public int delete(Long[] ids) {
        return 0;
    }

    @Override
    public Page<ModifyOrderLogs> list(Pageable pageable, Map params) {
        Specification specification = DynamicSpecifications.bySearchFilter(SearchFilter.parse(params).values(), ModifyOrderLogs.class);
        Page<ModifyOrderLogs> result = modifyOrderLogsRepo.findAll(specification, pageable);
        return result;
    }

    @Override
    public ModifyOrderLogs info(Long id) {
        return null;
    }

    public void save(Order oldOrder, Order newOrder, Account account) {
        List<ModifyOrderLogs> modifyOrderLogsArrayList = Lists.newArrayList();
        if (oldOrder != null && newOrder != null){
            //判断实体的修改
            BeanChangeUtil<T> beanChangeUtil = new BeanChangeUtil<>();
            Map<String, Object> stringObjectMap = beanChangeUtil.contrastObj(oldOrder, newOrder);

            if (account != null){
                for (Map.Entry<String, Object> entry : stringObjectMap.entrySet()){
                    ModifyOrderLogs modifyOrderLogs = new ModifyOrderLogs();
                    modifyOrderLogsArrayList.add(modifyOrderLogs);

                    modifyOrderLogs.setModifyTime(LocalDateTime.now());
                    modifyOrderLogs.setModifier(account.getName());
                    modifyOrderLogs.setFieldName(entry.getKey());
                    modifyOrderLogs.setFieldValue(entry.getValue().toString());
                    //获取字段名
                    for (Map.Entry<String, String> fieldName : OrderDict.fieldNameMaps.entrySet()) {
                        if (entry.getKey().equals(fieldName.getKey())){
                            modifyOrderLogs.setFieldName(fieldName.getValue());
                        }
                    }
                    //获取阶段名
                    for (Map.Entry<String, String> stage : OrderDict.stageMaps.entrySet()) {
                        if (entry.getKey().equals(stage.getKey())){
                            modifyOrderLogs.setModifyStage(stage.getValue());
                        }
                    }
                }
            }
        }
        if (modifyOrderLogsArrayList.size() > 0){
            modifyOrderLogsRepo.saveAll(modifyOrderLogsArrayList);
        }
    }
}
